package com.ext.aic.components;

import android.content.Context;
import android.view.View;
import android.widget.Button;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ext.aic.R;
import com.ext.aic.adapter.ReferenceCopyListAdapter;
import com.ext.aic.beans.ProductItem;
import com.ext.aic.constants.EBusConstants;
import com.ext.aic.utils.ToastyUtils;
import com.eyinfo.android_pure_utils.ObjectManager;
import com.eyinfo.android_pure_utils.ebus.EBus;
import com.eyinfo.android_pure_utils.utils.ModelConvert;
import com.lxj.xpopup.core.BottomPopupView;

public class ReferenceCopyBottomPopup extends BottomPopupView {

    private ReferenceCopyListAdapter adapter;
    private ProductItem selectedItem;

    public ReferenceCopyBottomPopup(Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.copy_writing_select_layout;
    }

    @Override
    public int getMinimumHeight() {
        return 200;
    }

    @Override
    protected int getMaxHeight() {
        return ObjectManager.getScreenHeight(getContext()) * 4 / 5;
    }

    @Override
    protected void onCreate() {
        RecyclerView rv = findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new ReferenceCopyListAdapter(getContext());
        adapter.setItemAction((item) -> selectedItem = ModelConvert.toModel(item, ProductItem.class));
        rv.setAdapter(adapter);

        Button nextStepBtn = findViewById(R.id.next_btn);
        nextStepBtn.setOnClickListener(this::onNextStepClick);
        Button cancelBtn = findViewById(R.id.cancel_btn);
        cancelBtn.setOnClickListener((v) -> dismiss());
    }

    public void onNextStepClick(View view) {
        if (selectedItem == null) {
            ToastyUtils.getInstance().show(getContext().getString(R.string.select_reference_copy_hint));
            return;
        }
        EBus.getInstance().post(EBusConstants.selectReferenceCopyBus, selectedItem);
    }
}
